package com.simple;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

//生产者 发送消息
public class product {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.建立连接
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("121.37.137.119");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("study");
        connectionFactory.setPassword("study");
        connectionFactory.setVirtualHost("bite");
        Connection connection = connectionFactory.newConnection();
        //2.开启通道
        Channel channel = connection.createChannel();
        //3.声明交换机
        //4.声明队列
        /**
         * queueDeclare(String var1, boolean var2, boolean var3, boolean var4, Map<String, Object> var5)
         *     参数说明:       队列名称       可持久化    是否独占        是否自动删除      参数
         */
        channel.queueDeclare("hello",true,false,false,null);
        //5.发送消息
        //发送10条消息到rabbitmq
        for(int i=0;i<10;i++){
            String msg = "hello "+i ;
            /**
             * basicPublish(String var1, String var2, AMQP.BasicProperties var3, byte[] var4)
             * 参数说明         交换机名称  内置交换机(要和队列名一致)          属性配置         消息
             */
            channel.basicPublish("","hello",null,msg.getBytes());
        }
        //6.释放资源
        //(这两个资源关闭的顺序不能反,channel必须在connection前面,或者只关闭connection.
        channel.close();
        connection.close();
    }
}
